/**
 * 
 */
package com.fbty.mt.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;



/**
 * @author freedom
 *
 */
public class IPInfoHelper {
	
	private final static String CHARSET = "utf-8";
	
	/**
	 * 
	 * @param ip
	 * @return map = {Region=33(地区), Host name=106.81.26.211(地址或域名), Latitude=29.5628, Longitude=106.5528, B Class=106.81.0.0 - 106.81.255.255(请求IP所在IP段), Country=China(国家), City=Chongqing(城市)}
	 */
	public static Map<String, String> getIPInfo(String ip){
		
		String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."                  
					+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."                  
					+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."                  
					+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$"; 
		if(ip.matches(regex)){
			System.out.println("--");
		}else{
			System.err.println(ip+" 不是一个合法的IP地址");
		}
		
		Map<String, String> map = new HashMap<String, String>();
		
		HttpClient client = new HttpClient();
		
		GetMethod method = new GetMethod("http://ip-address-lookup-v4.com/lookup.php?ip="+ip+"+&host=ip-address-lookup-v4.com&x=67&y=7");
		
		try {
			client.executeMethod(method);
			if(method.getStatusLine().getStatusCode()==200){
				//String body = method.getResponseBodyAsString();
				
				InputStream input = method.getResponseBodyAsStream();
				BufferedReader reader = new BufferedReader(new InputStreamReader(input,CHARSET)); 
				String line = null;
				StringBuffer sb = new StringBuffer();
				while ((line = reader.readLine()) != null) {   
					 sb.append(line).append("\r\n");   
				}   
				
				String body = sb.toString();
				
				Parser parser = Parser.createParser(body, CHARSET);
				AndFilter filter =  new AndFilter(new TagNameFilter("div"),new HasAttributeFilter("class","mapdetails"));   
				NodeList list = parser.parse(filter);
				
				Node node = null;
				for (int i = 0; i < list.size(); i++){
					node = list.elementAt(i);
				}
				
				String content = node.toHtml();
				parser = Parser.createParser(content, CHARSET);
				AndFilter mapcat =  new AndFilter(new TagNameFilter("div"),new HasAttributeFilter("class","mapcat"));
				NodeList catList = parser.parse(mapcat);
				
				parser = Parser.createParser(content, CHARSET);
				AndFilter mapvalue =  new AndFilter(new TagNameFilter("div"),new HasAttributeFilter("class","mapvalue"));
				NodeList valueList = parser.parse(mapvalue);
				
				
				for (int i = 0; i < catList.size(); i++){
					Node key = catList.elementAt(i);
					Node value = valueList.elementAt(i);
					String str = key.toPlainTextString().trim();
					map.put(str.substring(0, str.length()-1), value.toPlainTextString().trim());
				}
				
				System.out.println(map);
			}
			
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserException e) {
			e.printStackTrace();
		}finally {
        	method.releaseConnection();
        }
		return map;
		
	}
	
	public static void main(String[] args) {
		IPInfoHelper.getIPInfo("75.150.141.65");
		
		
		
	}

}
